<html>
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
  <title>
   Python 的 Socket 编程教程  | 数螺 | NAUT IDEA
  </title>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet"/>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
  <style type="text/css">
   #xmain img {
                  max-width: 100%;
                  display: block;
                  margin-top: 10px;
                  margin-bottom: 10px;
                }

                #xmain p {
                    line-height:150%;
                    font-size: 16px;
                    margin-top: 20px;
                }

                #xmain h2 {
                    font-size: 24px;
                }

                #xmain h3 {
                    font-size: 20px;
                }

                #xmain h4 {
                    font-size: 18px;
                }


                .header {
	           background-color: #0099ff;
	           color: #ffffff;
	           margin-bottom: 20px;
	        }

	        .header p {
                  margin: 0px;
                  padding: 10px 0;
                  display: inline-block;  
                  vertical-align: middle;
                  font-size: 16px;
               }

               .header a {
                 color: white;
               }

              .header img {
                 height: 25px;
              }
  </style>
  <script src="http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js">
  </script>
  <script src="http://nautstatic-10007657.file.myqcloud.com/static/css/readability.min.js" type="text/javascript">
  </script>
  <script type="text/javascript">
   $(document).ready(function() {
                 var loc = document.location;
                 var uri = {
                  spec: "http://dataunion.org/24438.html",
                  host: "http://dataunion.org",
                  prePath: "http://dataunion.org",
                  scheme: "http",
                  pathBase: "http://dataunion.org/"
                 };
    
                 var documentClone = document.cloneNode(true);
                 var article = new Readability(uri, documentClone).parse();
     
                 document.getElementById("xmain").innerHTML = article.content;
                });
  </script>
  <!-- 1466460724: Accept with keywords: (title(0.2):Python,教程,编程,数盟,社区, topn(0.266666666667):社区,浏览器,端口,接下来,数盟,行业资讯,数据挖掘,函数,示例,Python,例子,终端,文章,服务器,数据,教程,服务器端,python,编程,地址,发送数据,接收数据,客户端,通讯,编程语言,数据包,代码,程序,线程,命令行).-->
 </head>
 <body onload="">
  <div class="header">
   <div class="container">
    <div class="row">
     <div class="col-xs-6 col-sm-6 text-left">
      <a href="/databee">
       <img src="http://nautidea-10007657.cos.myqcloud.com/logo_white.png"/>
      </a>
      <a href="/databee">
       <p>
        数螺
       </p>
      </a>
     </div>
     <div class="hidden-xs col-sm-6 text-right">
      <p>
       致力于数据科学的推广和知识传播
      </p>
     </div>
    </div>
   </div>
  </div>
  <div class="container text-center">
   <h1>
    Python 的 Socket 编程教程
   </h1>
  </div>
  <div class="container" id="xmain">
   ﻿﻿
   <title>
    Python 的 Socket 编程教程 | 数盟社区
   </title>
   <!-- All in One SEO Pack 2.2.7.6.2 by Michael Torbert of Semper Fi Web Design[32,67] -->
   <!-- /all in one seo pack -->
   <!--
<div align="center">
<a href="http://strata.oreilly.com.cn/hadoop-big-data-cn?cmp=mp-data-confreg-home-stcn16_dataunion_pc" target="_blank"><img src="http://dataunion.org/wp-content/uploads/2016/05/stratabj.jpg"/ ></a>
</div>
-->
   <header id="header-web">
    <div class="header-main">
     <hgroup class="logo">
      <h1>
       <a href="http://dataunion.org/" rel="home" title="数盟社区">
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/logo.png"/>
       </a>
      </h1>
     </hgroup>
     <!--logo-->
     <nav class="header-nav">
      <ul class="menu" id="menu-%e4%b8%bb%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-71" id="menu-item-71">
        <a href="http://dataunion.org/category/events" title="events">
         活动
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22457" id="menu-item-22457">
          <a href="http://dataunion.org/2016timeline">
           2016档期
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22459" id="menu-item-22459">
          <a href="http://dataunion.org/category/parterc">
           合作会议
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor menu-item-has-children menu-item-20869" id="menu-item-20869">
        <a href="http://dataunion.org/category/tech" title="articles">
         文章
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20867" id="menu-item-20867">
          <a href="http://dataunion.org/category/tech/base" title="base">
           基础架构
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3302" id="menu-item-3302">
          <a href="http://dataunion.org/category/tech/ai" title="ai">
           人工智能
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3303" id="menu-item-3303">
          <a href="http://dataunion.org/category/tech/analysis" title="analysis">
           数据分析
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21920" id="menu-item-21920">
          <a href="http://dataunion.org/category/tech/dm">
           数据挖掘
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3314" id="menu-item-3314">
          <a href="http://dataunion.org/category/tech/viz" title="viz">
           可视化
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-3305" id="menu-item-3305">
          <a href="http://dataunion.org/category/tech/devl" title="devl">
           编程语言
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-20876" id="menu-item-20876">
        <a href="http://dataunion.org/category/industry">
         行业
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-16328" id="menu-item-16328">
          <a href="http://dataunion.org/category/industry/case" title="case">
           行业应用
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2112" id="menu-item-2112">
          <a href="http://dataunion.org/category/industry/demo" title="demo">
           Demo展示
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21562" id="menu-item-21562">
          <a href="http://dataunion.org/category/industry/news">
           行业资讯
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-311" id="menu-item-311">
        <a href="http://dataunion.org/category/sources" title="sources">
         资源
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20870" id="menu-item-20870">
        <a href="http://dataunion.org/category/books" title="book">
         图书
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21363" id="menu-item-21363">
        <a href="http://dataunion.org/category/training">
         课程
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-21853" id="menu-item-21853">
        <a href="http://dataunion.org/category/jobs">
         职位
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22050" id="menu-item-22050">
          <a href="http://dataunion.org/category/career">
           职业规划
          </a>
         </li>
        </ul>
       </li>
      </ul>
     </nav>
     <!--header-nav-->
    </div>
   </header>
   <!--header-web-->
   <div id="main">
    <div id="soutab">
     <form action="http://dataunion.org/" class="search" method="get">
     </form>
    </div>
    <div id="container">
     <nav id="mbx">
      当前位置：
      <a href="http://dataunion.org">
       首页
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech">
       文章
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech/devl">
       编程语言
      </a>
      &gt;  正文
     </nav>
     <!--mbx-->
     <article class="content">
      <header align="centre" class="contenttitle">
       <div class="mscc">
        <h1 class="mscctitle">
         <a href="http://dataunion.org/24438.html">
          Python 的 Socket 编程教程
         </a>
        </h1>
        <address class="msccaddress ">
         <em>
          789 次阅读 -
         </em>
         <a href="http://dataunion.org/category/tech/devl" rel="category tag">
          编程语言
         </a>
        </address>
       </div>
      </header>
      <div class="content-text">
       <p>
        这是用来快速学习 Python Socket 套接字编程的指南和教程。Python 的 Socket 编程跟 C 语言很像。
       </p>
       <p>
        Python 官方关于 Socket 的函数请看
        <a href="http://docs.python.org/library/socket.html" rel="nofollow" target="_blank">
         http://docs.python.org/library/socket.html
        </a>
       </p>
       <p>
        基本上，Socket 是任何一种计算机网络通讯中最基础的内容。例如当你在浏览器地址栏中输入 www.oschina.net 时，你会打开一个套接字，然后连接到 www.oschina.net 并读取响应的页面然后然后显示出来。而其他一些聊天客户端如 gtalk 和 skype 也是类似。任何网络通讯都是通过 Socket 来完成的。
       </p>
       <h3>
        写在开头
       </h3>
       <p>
        本教程假设你已经有一些基本的 Python 编程的知识。
       </p>
       <p>
        让我们开始 Socket 编程吧。
       </p>
       <h3>
        创建 Socket
       </h3>
       <p>
        首先要做的就是创建一个 Socket，socket 的 socket 函数可以实现，代码如下：
       </p>
       <div>
        <div class="syntaxhighlighter python" id="highlighter_431108">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python comments">
                #Socket client example in python
               </code>
              </div>
              <div class="line number2 index1 alt1">
              </div>
              <div class="line number3 index2 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                socket
               </code>
               <code class="python comments">
                #for sockets
               </code>
              </div>
              <div class="line number4 index3 alt1">
              </div>
              <div class="line number5 index4 alt2">
               <code class="python comments">
                #create an AF_INET, STREAM socket (TCP)
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python plain">
                s
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)
               </code>
              </div>
              <div class="line number7 index6 alt2">
              </div>
              <div class="line number8 index7 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket Created'
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        函数 socket.socket 创建了一个 Socket，并返回 Socket 的描述符可用于其他 Socket 相关的函数。
       </p>
       <p>
        上述代码使用了下面两个属性来创建 Socket：
       </p>
       <p>
        地址簇 : AF_INET (IPv4)
        <br/>
        类型: SOCK_STREAM (使用 TCP 传输控制协议)
       </p>
       <p>
        <b>
         错误处理
        </b>
       </p>
       <p>
        如果 socket 函数失败了，python 将抛出一个名为 socket.error 的异常，这个异常必须予以处理：
       </p>
       <div>
        <div class="syntaxhighlighter python" id="highlighter_394472">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python comments">
                #handling errors in python socket programs
               </code>
              </div>
              <div class="line number2 index1 alt1">
              </div>
              <div class="line number3 index2 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                socket
               </code>
               <code class="python comments">
                #for sockets
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                sys
               </code>
               <code class="python comments">
                #for exit
               </code>
              </div>
              <div class="line number5 index4 alt2">
              </div>
              <div class="line number6 index5 alt1">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #create an AF_INET, STREAM socket (TCP)
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                s
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.error, msg:
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Failed to create socket. Error code: '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python functions">
                str
               </code>
               <code class="python plain">
                (msg[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ])
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' , Error message : '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                msg[
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit();
               </code>
              </div>
              <div class="line number12 index11 alt1">
              </div>
              <div class="line number13 index12 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket Created'
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        好了，假设你已经成功创建了 Socket，下一步该做什么呢？接下来我们将使用这个 Socket 来连接到服务器。
       </p>
       <p>
        <b>
         注意
        </b>
       </p>
       <p>
        与 SOCK_STREAM 相对应的其他类型是 SOCK_DGRAM 用于 UDP 通讯协议，UDP 通讯是非连接 Socket，在这篇文章中我们只讨论 SOCK_STREAM ，或者叫 TCP 。
       </p>
       <h3>
        连接到服务器
       </h3>
       <p>
        连接到服务器需要服务器地址和端口号，这里使用的是 www.oschina.net 和 80 端口。
       </p>
       <p>
        <b>
         首先获取远程主机的 IP 地址
        </b>
       </p>
       <p>
        连接到远程主机之前，我们需要知道它的 IP 地址，在 Python 中，获取 IP 地址是很简单的：
       </p>
       <div>
        <div class="syntaxhighlighter python" id="highlighter_395869">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
             <div class="line number14 index13 alt1">
              14
             </div>
             <div class="line number15 index14 alt2">
              15
             </div>
             <div class="line number16 index15 alt1">
              16
             </div>
             <div class="line number17 index16 alt2">
              17
             </div>
             <div class="line number18 index17 alt1">
              18
             </div>
             <div class="line number19 index18 alt2">
              19
             </div>
             <div class="line number20 index19 alt1">
              20
             </div>
             <div class="line number21 index20 alt2">
              21
             </div>
             <div class="line number22 index21 alt1">
              22
             </div>
             <div class="line number23 index22 alt2">
              23
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                socket
               </code>
               <code class="python comments">
                #for sockets
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                sys
               </code>
               <code class="python comments">
                #for exit
               </code>
              </div>
              <div class="line number3 index2 alt2">
              </div>
              <div class="line number4 index3 alt1">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #create an AF_INET, STREAM socket (TCP)
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                s
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.error, msg:
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Failed to create socket. Error code: '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python functions">
                str
               </code>
               <code class="python plain">
                (msg[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ])
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' , Error message : '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                msg[
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit();
               </code>
              </div>
              <div class="line number10 index9 alt1">
              </div>
              <div class="line number11 index10 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket Created'
               </code>
              </div>
              <div class="line number12 index11 alt1">
              </div>
              <div class="line number13 index12 alt2">
               <code class="python plain">
                host
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                'www.oschina.net'
               </code>
              </div>
              <div class="line number14 index13 alt1">
              </div>
              <div class="line number15 index14 alt2">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number16 index15 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                remote_ip
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.gethostbyname( host )
               </code>
              </div>
              <div class="line number17 index16 alt2">
              </div>
              <div class="line number18 index17 alt1">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.gaierror:
               </code>
              </div>
              <div class="line number19 index18 alt2">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #could not resolve
               </code>
              </div>
              <div class="line number20 index19 alt1">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Hostname could not be resolved. Exiting'
               </code>
              </div>
              <div class="line number21 index20 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit()
               </code>
              </div>
              <div class="line number22 index21 alt1">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number23 index22 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Ip address of '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                host
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' is '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                remote_ip
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        我们已经有 IP 地址了，接下来需要指定要连接的端口。
       </p>
       <p>
        代码：
       </p>
       <div>
        <div class="syntaxhighlighter python" id="highlighter_912227">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
             <div class="line number14 index13 alt1">
              14
             </div>
             <div class="line number15 index14 alt2">
              15
             </div>
             <div class="line number16 index15 alt1">
              16
             </div>
             <div class="line number17 index16 alt2">
              17
             </div>
             <div class="line number18 index17 alt1">
              18
             </div>
             <div class="line number19 index18 alt2">
              19
             </div>
             <div class="line number20 index19 alt1">
              20
             </div>
             <div class="line number21 index20 alt2">
              21
             </div>
             <div class="line number22 index21 alt1">
              22
             </div>
             <div class="line number23 index22 alt2">
              23
             </div>
             <div class="line number24 index23 alt1">
              24
             </div>
             <div class="line number25 index24 alt2">
              25
             </div>
             <div class="line number26 index25 alt1">
              26
             </div>
             <div class="line number27 index26 alt2">
              27
             </div>
             <div class="line number28 index27 alt1">
              28
             </div>
             <div class="line number29 index28 alt2">
              29
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                socket
               </code>
               <code class="python comments">
                #for sockets
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                sys
               </code>
               <code class="python comments">
                #for exit
               </code>
              </div>
              <div class="line number3 index2 alt2">
              </div>
              <div class="line number4 index3 alt1">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #create an AF_INET, STREAM socket (TCP)
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                s
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.error, msg:
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Failed to create socket. Error code: '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python functions">
                str
               </code>
               <code class="python plain">
                (msg[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ])
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' , Error message : '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                msg[
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit();
               </code>
              </div>
              <div class="line number10 index9 alt1">
              </div>
              <div class="line number11 index10 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket Created'
               </code>
              </div>
              <div class="line number12 index11 alt1">
              </div>
              <div class="line number13 index12 alt2">
               <code class="python plain">
                host
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                'www.oschina.net'
               </code>
              </div>
              <div class="line number14 index13 alt1">
               <code class="python plain">
                port
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                80
               </code>
              </div>
              <div class="line number15 index14 alt2">
              </div>
              <div class="line number16 index15 alt1">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number17 index16 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                remote_ip
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.gethostbyname( host )
               </code>
              </div>
              <div class="line number18 index17 alt1">
              </div>
              <div class="line number19 index18 alt2">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.gaierror:
               </code>
              </div>
              <div class="line number20 index19 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #could not resolve
               </code>
              </div>
              <div class="line number21 index20 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Hostname could not be resolved. Exiting'
               </code>
              </div>
              <div class="line number22 index21 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit()
               </code>
              </div>
              <div class="line number23 index22 alt2">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number24 index23 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Ip address of '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                host
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' is '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                remote_ip
               </code>
              </div>
              <div class="line number25 index24 alt2">
              </div>
              <div class="line number26 index25 alt1">
               <code class="python comments">
                #Connect to remote server
               </code>
              </div>
              <div class="line number27 index26 alt2">
               <code class="python plain">
                s.connect((remote_ip , port))
               </code>
              </div>
              <div class="line number28 index27 alt1">
              </div>
              <div class="line number29 index28 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket Connected to '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                host
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' on ip '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                remote_ip
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        现在运行程序
       </p>
       <div>
        <div class="syntaxhighlighter shell" id="highlighter_333569">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="shell plain">
                $ python client.py
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="shell plain">
                Socket Created
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="shell plain">
                Ip address of www.oschina.net is 61.145.122.155
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="shell plain">
                Socket Connected to www.oschina.net on ip 61.145.122.155
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <div class="highlight">
        <p>
         这段程序创建了一个 Socket 并进行连接，试试使用其他一些不存在的端口（如81）会是怎样？这个逻辑相当于构建了一个端口扫描器。
        </p>
       </div>
       <p>
        已经连接上了，接下来就是往服务器上发送数据。
       </p>
       <p>
        <b>
         免费提示
        </b>
       </p>
       <p>
        使用 SOCK_STREAM/TCP 套接字才有“连接”的概念。连接意味着可靠的数据流通讯机制，可以同时有多个数据流。可以想象成一个数据互不干扰的管道。另外一个重要的提示是：数据包的发送和接收是有顺序的。
       </p>
       <p>
        其他一些 Socket 如 UDP、ICMP 和 ARP 没有“连接”的概念，它们是无连接通讯，意味着你可从任何人或者给任何人发送和接收数据包。
       </p>
       <h3>
        发送数据
       </h3>
       <p>
        sendall 函数用于简单的发送数据，我们来向 oschina 发送一些数据：
       </p>
       <div>
        <div class="syntaxhighlighter python" id="highlighter_825340">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
             <div class="line number14 index13 alt1">
              14
             </div>
             <div class="line number15 index14 alt2">
              15
             </div>
             <div class="line number16 index15 alt1">
              16
             </div>
             <div class="line number17 index16 alt2">
              17
             </div>
             <div class="line number18 index17 alt1">
              18
             </div>
             <div class="line number19 index18 alt2">
              19
             </div>
             <div class="line number20 index19 alt1">
              20
             </div>
             <div class="line number21 index20 alt2">
              21
             </div>
             <div class="line number22 index21 alt1">
              22
             </div>
             <div class="line number23 index22 alt2">
              23
             </div>
             <div class="line number24 index23 alt1">
              24
             </div>
             <div class="line number25 index24 alt2">
              25
             </div>
             <div class="line number26 index25 alt1">
              26
             </div>
             <div class="line number27 index26 alt2">
              27
             </div>
             <div class="line number28 index27 alt1">
              28
             </div>
             <div class="line number29 index28 alt2">
              29
             </div>
             <div class="line number30 index29 alt1">
              30
             </div>
             <div class="line number31 index30 alt2">
              31
             </div>
             <div class="line number32 index31 alt1">
              32
             </div>
             <div class="line number33 index32 alt2">
              33
             </div>
             <div class="line number34 index33 alt1">
              34
             </div>
             <div class="line number35 index34 alt2">
              35
             </div>
             <div class="line number36 index35 alt1">
              36
             </div>
             <div class="line number37 index36 alt2">
              37
             </div>
             <div class="line number38 index37 alt1">
              38
             </div>
             <div class="line number39 index38 alt2">
              39
             </div>
             <div class="line number40 index39 alt1">
              40
             </div>
             <div class="line number41 index40 alt2">
              41
             </div>
             <div class="line number42 index41 alt1">
              42
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                socket
               </code>
               <code class="python comments">
                #for sockets
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                sys
               </code>
               <code class="python comments">
                #for exit
               </code>
              </div>
              <div class="line number3 index2 alt2">
              </div>
              <div class="line number4 index3 alt1">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #create an AF_INET, STREAM socket (TCP)
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                s
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.error, msg:
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Failed to create socket. Error code: '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python functions">
                str
               </code>
               <code class="python plain">
                (msg[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ])
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' , Error message : '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                msg[
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit();
               </code>
              </div>
              <div class="line number10 index9 alt1">
              </div>
              <div class="line number11 index10 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket Created'
               </code>
              </div>
              <div class="line number12 index11 alt1">
              </div>
              <div class="line number13 index12 alt2">
               <code class="python plain">
                host
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                'www.oschina.net'
               </code>
              </div>
              <div class="line number14 index13 alt1">
               <code class="python plain">
                port
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                80
               </code>
              </div>
              <div class="line number15 index14 alt2">
              </div>
              <div class="line number16 index15 alt1">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number17 index16 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                remote_ip
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.gethostbyname( host )
               </code>
              </div>
              <div class="line number18 index17 alt1">
              </div>
              <div class="line number19 index18 alt2">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.gaierror:
               </code>
              </div>
              <div class="line number20 index19 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #could not resolve
               </code>
              </div>
              <div class="line number21 index20 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Hostname could not be resolved. Exiting'
               </code>
              </div>
              <div class="line number22 index21 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit()
               </code>
              </div>
              <div class="line number23 index22 alt2">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number24 index23 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Ip address of '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                host
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' is '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                remote_ip
               </code>
              </div>
              <div class="line number25 index24 alt2">
              </div>
              <div class="line number26 index25 alt1">
               <code class="python comments">
                #Connect to remote server
               </code>
              </div>
              <div class="line number27 index26 alt2">
               <code class="python plain">
                s.connect((remote_ip , port))
               </code>
              </div>
              <div class="line number28 index27 alt1">
              </div>
              <div class="line number29 index28 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket Connected to '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                host
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' on ip '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                remote_ip
               </code>
              </div>
              <div class="line number30 index29 alt1">
              </div>
              <div class="line number31 index30 alt2">
               <code class="python comments">
                #Send some data to remote server
               </code>
              </div>
              <div class="line number32 index31 alt1">
               <code class="python plain">
                message
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                "GET / HTTP/1.1\r\n\r\n"
               </code>
              </div>
              <div class="line number33 index32 alt2">
              </div>
              <div class="line number34 index33 alt1">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number35 index34 alt2">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #Set the whole string
               </code>
              </div>
              <div class="line number36 index35 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                s.sendall(message)
               </code>
              </div>
              <div class="line number37 index36 alt2">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.error:
               </code>
              </div>
              <div class="line number38 index37 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #Send failed
               </code>
              </div>
              <div class="line number39 index38 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Send failed'
               </code>
              </div>
              <div class="line number40 index39 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit()
               </code>
              </div>
              <div class="line number41 index40 alt2">
              </div>
              <div class="line number42 index41 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Message send successfully'
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        上述例子中，首先连接到目标服务器，然后发送字符串数据 “GET / HTTP/1.1\r\n\r\n” ，这是一个 HTTP 协议的命令，用来获取网站首页的内容。
       </p>
       <p>
        接下来需要读取服务器返回的数据。
       </p>
       <h3>
        接收数据
       </h3>
       <p>
        recv 函数用于从 socket 接收数据：
       </p>
       <div>
        <div class="syntaxhighlighter python" id="highlighter_794475">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
             <div class="line number14 index13 alt1">
              14
             </div>
             <div class="line number15 index14 alt2">
              15
             </div>
             <div class="line number16 index15 alt1">
              16
             </div>
             <div class="line number17 index16 alt2">
              17
             </div>
             <div class="line number18 index17 alt1">
              18
             </div>
             <div class="line number19 index18 alt2">
              19
             </div>
             <div class="line number20 index19 alt1">
              20
             </div>
             <div class="line number21 index20 alt2">
              21
             </div>
             <div class="line number22 index21 alt1">
              22
             </div>
             <div class="line number23 index22 alt2">
              23
             </div>
             <div class="line number24 index23 alt1">
              24
             </div>
             <div class="line number25 index24 alt2">
              25
             </div>
             <div class="line number26 index25 alt1">
              26
             </div>
             <div class="line number27 index26 alt2">
              27
             </div>
             <div class="line number28 index27 alt1">
              28
             </div>
             <div class="line number29 index28 alt2">
              29
             </div>
             <div class="line number30 index29 alt1">
              30
             </div>
             <div class="line number31 index30 alt2">
              31
             </div>
             <div class="line number32 index31 alt1">
              32
             </div>
             <div class="line number33 index32 alt2">
              33
             </div>
             <div class="line number34 index33 alt1">
              34
             </div>
             <div class="line number35 index34 alt2">
              35
             </div>
             <div class="line number36 index35 alt1">
              36
             </div>
             <div class="line number37 index36 alt2">
              37
             </div>
             <div class="line number38 index37 alt1">
              38
             </div>
             <div class="line number39 index38 alt2">
              39
             </div>
             <div class="line number40 index39 alt1">
              40
             </div>
             <div class="line number41 index40 alt2">
              41
             </div>
             <div class="line number42 index41 alt1">
              42
             </div>
             <div class="line number43 index42 alt2">
              43
             </div>
             <div class="line number44 index43 alt1">
              44
             </div>
             <div class="line number45 index44 alt2">
              45
             </div>
             <div class="line number46 index45 alt1">
              46
             </div>
             <div class="line number47 index46 alt2">
              47
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python comments">
                #Socket client example in python
               </code>
              </div>
              <div class="line number2 index1 alt1">
              </div>
              <div class="line number3 index2 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                socket
               </code>
               <code class="python comments">
                #for sockets
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                sys
               </code>
               <code class="python comments">
                #for exit
               </code>
              </div>
              <div class="line number5 index4 alt2">
              </div>
              <div class="line number6 index5 alt1">
               <code class="python comments">
                #create an INET, STREAMing socket
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                s
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.error:
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Failed to create socket'
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit()
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number13 index12 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket Created'
               </code>
              </div>
              <div class="line number14 index13 alt1">
              </div>
              <div class="line number15 index14 alt2">
               <code class="python plain">
                host
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                'oschina.net'
               </code>
               <code class="python plain">
                ;
               </code>
              </div>
              <div class="line number16 index15 alt1">
               <code class="python plain">
                port
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                80
               </code>
               <code class="python plain">
                ;
               </code>
              </div>
              <div class="line number17 index16 alt2">
              </div>
              <div class="line number18 index17 alt1">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number19 index18 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                remote_ip
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.gethostbyname( host )
               </code>
              </div>
              <div class="line number20 index19 alt1">
              </div>
              <div class="line number21 index20 alt2">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.gaierror:
               </code>
              </div>
              <div class="line number22 index21 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #could not resolve
               </code>
              </div>
              <div class="line number23 index22 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Hostname could not be resolved. Exiting'
               </code>
              </div>
              <div class="line number24 index23 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit()
               </code>
              </div>
              <div class="line number25 index24 alt2">
              </div>
              <div class="line number26 index25 alt1">
               <code class="python comments">
                #Connect to remote server
               </code>
              </div>
              <div class="line number27 index26 alt2">
               <code class="python plain">
                s.connect((remote_ip , port))
               </code>
              </div>
              <div class="line number28 index27 alt1">
              </div>
              <div class="line number29 index28 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket Connected to '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                host
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' on ip '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                remote_ip
               </code>
              </div>
              <div class="line number30 index29 alt1">
              </div>
              <div class="line number31 index30 alt2">
               <code class="python comments">
                #Send some data to remote server
               </code>
              </div>
              <div class="line number32 index31 alt1">
               <code class="python plain">
                message
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                "GET / HTTP/1.1\r\nHost: oschina.net\r\n\r\n"
               </code>
              </div>
              <div class="line number33 index32 alt2">
              </div>
              <div class="line number34 index33 alt1">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number35 index34 alt2">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #Set the whole string
               </code>
              </div>
              <div class="line number36 index35 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                s.sendall(message)
               </code>
              </div>
              <div class="line number37 index36 alt2">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.error:
               </code>
              </div>
              <div class="line number38 index37 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #Send failed
               </code>
              </div>
              <div class="line number39 index38 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Send failed'
               </code>
              </div>
              <div class="line number40 index39 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit()
               </code>
              </div>
              <div class="line number41 index40 alt2">
              </div>
              <div class="line number42 index41 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Message send successfully'
               </code>
              </div>
              <div class="line number43 index42 alt2">
              </div>
              <div class="line number44 index43 alt1">
               <code class="python comments">
                #Now receive data
               </code>
              </div>
              <div class="line number45 index44 alt2">
               <code class="python plain">
                reply
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                s.recv(
               </code>
               <code class="python value">
                4096
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number46 index45 alt1">
              </div>
              <div class="line number47 index46 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python plain">
                reply
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        下面是上述程序执行的结果：
       </p>
       <div>
        <div class="syntaxhighlighter xml" id="highlighter_199626">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="xml plain">
                $ python client.py
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="xml plain">
                Socket Created
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="xml plain">
                Ip address of oschina.net is 61.145.122.
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="xml plain">
                Socket Connected to oschina.net on ip 61.145.122.155
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="xml plain">
                Message send successfully
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="xml plain">
                HTTP/1.1 301 Moved Permanently
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="xml plain">
                Server: nginx
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="xml plain">
                Date: Wed, 24 Oct 2012 13:26:46 GMT
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="xml plain">
                Content-Type: text/html
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="xml plain">
                Content-Length: 178
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="xml plain">
                Connection: keep-alive
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="xml plain">
                Keep-Alive: timeout=20
               </code>
              </div>
              <div class="line number13 index12 alt2">
               <code class="xml plain">
                Location:
                <a href="http://www.oschina.net/">
                 http://www.oschina.net/
                </a>
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        oschina.net 回应了我们所请求的 URL 的内容，很简单。数据接收完了，可以关闭 Socket 了。
       </p>
       <h3>
        关闭 socket
       </h3>
       <p>
        close 函数用于关闭 Socket：
       </p>
       <div>
        <div class="syntaxhighlighter python" id="highlighter_618552">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                s.close()
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        这就是了。
       </p>
       <h3>
        让我们回顾一下
       </h3>
       <p>
        上述的示例中我们学到了如何：
       </p>
       <p>
        1. 创建 Socket
        <br/>
        2. 连接到远程服务器
        <br/>
        3. 发送数据
        <br/>
        4. 接收回应
       </p>
       <p>
        当你用浏览器打开 www.oschina.net 时，其过程也是一样。包含两种类型，分别是客户端和服务器，客户端连接到服务器并读取数据，服务器使用 Socket 接收进入的连接并提供数据。因此在这里 www.oschina.net 是服务器端，而你的浏览器是客户端。
       </p>
       <p>
        接下来我们开始在服务器端做点编码。
       </p>
       <h3>
        服务器端编程
       </h3>
       <p>
        服务器端编程主要包括下面几步：
       </p>
       <p>
        1. 打开 socket
        <br/>
        2. 绑定到一个地址和端口
        <br/>
        3. 侦听进来的连接
        <br/>
        4. 接受连接
        <br/>
        5. 读写数据
       </p>
       <p>
        我们已经学习过如何打开 Socket 了，下面是绑定到指定的地址和端口上。
       </p>
       <h3>
        绑定 Socket
       </h3>
       <p>
        bind 函数用于将 Socket 绑定到一个特定的地址和端口，它需要一个类似 connect 函数所需的 sockaddr_in 结构体。
       </p>
       <p>
        示例代码：
       </p>
       <div>
        <div class="syntaxhighlighter python" id="highlighter_387193">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
             <div class="line number14 index13 alt1">
              14
             </div>
             <div class="line number15 index14 alt2">
              15
             </div>
             <div class="line number16 index15 alt1">
              16
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                socket
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                sys
               </code>
              </div>
              <div class="line number3 index2 alt2">
              </div>
              <div class="line number4 index3 alt1">
               <code class="python plain">
                HOST
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                ''
               </code>
               <code class="python comments">
                # Symbolic name meaning all available interfaces
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python plain">
                PORT
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                8888
               </code>
               <code class="python comments">
                # Arbitrary non-privileged port
               </code>
              </div>
              <div class="line number6 index5 alt1">
              </div>
              <div class="line number7 index6 alt2">
               <code class="python plain">
                s
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket created'
               </code>
              </div>
              <div class="line number9 index8 alt2">
              </div>
              <div class="line number10 index9 alt1">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                s.bind((HOST, PORT))
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.error , msg:
               </code>
              </div>
              <div class="line number13 index12 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Bind failed. Error Code : '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python functions">
                str
               </code>
               <code class="python plain">
                (msg[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ])
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' Message '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                msg[
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number14 index13 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit()
               </code>
              </div>
              <div class="line number15 index14 alt2">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number16 index15 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket bind complete'
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        绑定完成后，就需要让 Socket 开始侦听连接。很显然，你不能将两个不同的 Socket 绑定到同一个端口之上。
       </p>
       <h3>
        连接侦听
       </h3>
       <p>
        绑定 Socket 之后就可以开始侦听连接，我们需要将 Socket 变成侦听模式。socket 的 listen 函数用于实现侦听模式：
       </p>
       <div>
        <div class="syntaxhighlighter cpp" id="highlighter_223207">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="cpp plain">
                s.listen(10)
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="cpp plain">
                print
               </code>
               <code class="cpp string">
                'Socket now listening'
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        listen 函数所需的参数成为 backlog，用来控制程序忙时可保持等待状态的连接数。这里我们传递的是 10，意味着如果已经有 10 个连接在等待处理，那么第 11 个连接将会被拒绝。当检查了 socket_accept 后这个会更加清晰。
       </p>
       <h3>
        接受连接
       </h3>
       <p>
        示例代码：
       </p>
       <div>
        <div class="syntaxhighlighter python" id="highlighter_75664">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
             <div class="line number14 index13 alt1">
              14
             </div>
             <div class="line number15 index14 alt2">
              15
             </div>
             <div class="line number16 index15 alt1">
              16
             </div>
             <div class="line number17 index16 alt2">
              17
             </div>
             <div class="line number18 index17 alt1">
              18
             </div>
             <div class="line number19 index18 alt2">
              19
             </div>
             <div class="line number20 index19 alt1">
              20
             </div>
             <div class="line number21 index20 alt2">
              21
             </div>
             <div class="line number22 index21 alt1">
              22
             </div>
             <div class="line number23 index22 alt2">
              23
             </div>
             <div class="line number24 index23 alt1">
              24
             </div>
             <div class="line number25 index24 alt2">
              25
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                socket
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                sys
               </code>
              </div>
              <div class="line number3 index2 alt2">
              </div>
              <div class="line number4 index3 alt1">
               <code class="python plain">
                HOST
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                ''
               </code>
               <code class="python comments">
                # Symbolic name meaning all available interfaces
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python plain">
                PORT
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                8888
               </code>
               <code class="python comments">
                # Arbitrary non-privileged port
               </code>
              </div>
              <div class="line number6 index5 alt1">
              </div>
              <div class="line number7 index6 alt2">
               <code class="python plain">
                s
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket created'
               </code>
              </div>
              <div class="line number9 index8 alt2">
              </div>
              <div class="line number10 index9 alt1">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                s.bind((HOST, PORT))
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.error , msg:
               </code>
              </div>
              <div class="line number13 index12 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Bind failed. Error Code : '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python functions">
                str
               </code>
               <code class="python plain">
                (msg[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ])
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' Message '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                msg[
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number14 index13 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit()
               </code>
              </div>
              <div class="line number15 index14 alt2">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number16 index15 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket bind complete'
               </code>
              </div>
              <div class="line number17 index16 alt2">
              </div>
              <div class="line number18 index17 alt1">
               <code class="python plain">
                s.listen(
               </code>
               <code class="python value">
                10
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number19 index18 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket now listening'
               </code>
              </div>
              <div class="line number20 index19 alt1">
              </div>
              <div class="line number21 index20 alt2">
               <code class="python comments">
                #wait to accept a connection - blocking call
               </code>
              </div>
              <div class="line number22 index21 alt1">
               <code class="python plain">
                conn, addr
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                s.accept()
               </code>
              </div>
              <div class="line number23 index22 alt2">
              </div>
              <div class="line number24 index23 alt1">
               <code class="python comments">
                #display client information
               </code>
              </div>
              <div class="line number25 index24 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Connected with '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                addr[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ]
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ':'
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python functions">
                str
               </code>
               <code class="python plain">
                (addr[
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ])
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        <strong>
         输出
        </strong>
       </p>
       <p>
        运行该程序将会显示：
       </p>
       <div>
        <div class="syntaxhighlighter shell" id="highlighter_167384">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="shell plain">
                $ python server.py
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="shell plain">
                Socket created
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="shell plain">
                Socket bind complete
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="shell plain">
                Socket now listening
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        现在这个程序开始等待连接进入，端口是 8888，请不要关闭这个程序，我们来通过 telnet 程序来进行测试。
       </p>
       <p>
        打开命令行窗口并输入：
       </p>
       <div>
        <div class="syntaxhighlighter shell" id="highlighter_26018">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="shell plain">
                $ telnet localhost 8888
               </code>
              </div>
              <div class="line number2 index1 alt1">
              </div>
              <div class="line number3 index2 alt2">
               <code class="shell plain">
                It will immediately show
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="shell plain">
                $ telnet localhost 8888
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="shell plain">
                Trying 127.0.0.1...
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="shell plain">
                Connected to localhost.
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="shell plain">
                Escape character is
               </code>
               <code class="shell string">
                '^]'
               </code>
               <code class="shell plain">
                .
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="shell plain">
                Connection closed by foreign host.
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        而服务器端窗口显示的是：
       </p>
       <div>
        <div class="syntaxhighlighter shell" id="highlighter_396601">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="shell plain">
                $ python server.py
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="shell plain">
                Socket created
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="shell plain">
                Socket bind complete
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="shell plain">
                Socket now listening
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="shell plain">
                Connected with 127.0.0.1:59954
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        我们可看到客户端已经成功连接到服务器。
       </p>
       <p>
        上面例子我们接收到连接并立即关闭，这样的程序没什么实际的价值，连接建立后一般会有大量的事情需要处理，因此让我们来给客户端做出点回应吧。
       </p>
       <p>
        sendall 函数可通过 Socket 给客户端发送数据：
       </p>
       <div>
        <div class="syntaxhighlighter python" id="highlighter_457043">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
             <div class="line number14 index13 alt1">
              14
             </div>
             <div class="line number15 index14 alt2">
              15
             </div>
             <div class="line number16 index15 alt1">
              16
             </div>
             <div class="line number17 index16 alt2">
              17
             </div>
             <div class="line number18 index17 alt1">
              18
             </div>
             <div class="line number19 index18 alt2">
              19
             </div>
             <div class="line number20 index19 alt1">
              20
             </div>
             <div class="line number21 index20 alt2">
              21
             </div>
             <div class="line number22 index21 alt1">
              22
             </div>
             <div class="line number23 index22 alt2">
              23
             </div>
             <div class="line number24 index23 alt1">
              24
             </div>
             <div class="line number25 index24 alt2">
              25
             </div>
             <div class="line number26 index25 alt1">
              26
             </div>
             <div class="line number27 index26 alt2">
              27
             </div>
             <div class="line number28 index27 alt1">
              28
             </div>
             <div class="line number29 index28 alt2">
              29
             </div>
             <div class="line number30 index29 alt1">
              30
             </div>
             <div class="line number31 index30 alt2">
              31
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                socket
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                sys
               </code>
              </div>
              <div class="line number3 index2 alt2">
              </div>
              <div class="line number4 index3 alt1">
               <code class="python plain">
                HOST
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                ''
               </code>
               <code class="python comments">
                # Symbolic name meaning all available interfaces
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python plain">
                PORT
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                8888
               </code>
               <code class="python comments">
                # Arbitrary non-privileged port
               </code>
              </div>
              <div class="line number6 index5 alt1">
              </div>
              <div class="line number7 index6 alt2">
               <code class="python plain">
                s
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket created'
               </code>
              </div>
              <div class="line number9 index8 alt2">
              </div>
              <div class="line number10 index9 alt1">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                s.bind((HOST, PORT))
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.error , msg:
               </code>
              </div>
              <div class="line number13 index12 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Bind failed. Error Code : '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python functions">
                str
               </code>
               <code class="python plain">
                (msg[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ])
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' Message '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                msg[
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number14 index13 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit()
               </code>
              </div>
              <div class="line number15 index14 alt2">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number16 index15 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket bind complete'
               </code>
              </div>
              <div class="line number17 index16 alt2">
              </div>
              <div class="line number18 index17 alt1">
               <code class="python plain">
                s.listen(
               </code>
               <code class="python value">
                10
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number19 index18 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket now listening'
               </code>
              </div>
              <div class="line number20 index19 alt1">
              </div>
              <div class="line number21 index20 alt2">
               <code class="python comments">
                #wait to accept a connection - blocking call
               </code>
              </div>
              <div class="line number22 index21 alt1">
               <code class="python plain">
                conn, addr
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                s.accept()
               </code>
              </div>
              <div class="line number23 index22 alt2">
              </div>
              <div class="line number24 index23 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Connected with '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                addr[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ]
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ':'
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python functions">
                str
               </code>
               <code class="python plain">
                (addr[
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ])
               </code>
              </div>
              <div class="line number25 index24 alt2">
              </div>
              <div class="line number26 index25 alt1">
               <code class="python comments">
                #now keep talking with the client
               </code>
              </div>
              <div class="line number27 index26 alt2">
               <code class="python plain">
                data
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                conn.recv(
               </code>
               <code class="python value">
                1024
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number28 index27 alt1">
               <code class="python plain">
                conn.sendall(data)
               </code>
              </div>
              <div class="line number29 index28 alt2">
              </div>
              <div class="line number30 index29 alt1">
               <code class="python plain">
                conn.close()
               </code>
              </div>
              <div class="line number31 index30 alt2">
               <code class="python plain">
                s.close()
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        继续运行上述代码，然后打开另外一个命令行窗口输入下面命令：
       </p>
       <div>
        <div class="syntaxhighlighter bash" id="highlighter_151649">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="bash plain">
                $ telnet localhost 8888
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="bash plain">
                Trying 127.0.0.1...
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="bash plain">
                Connected to localhost.
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="bash plain">
                Escape character is
               </code>
               <code class="bash string">
                '^]'
               </code>
               <code class="bash plain">
                .
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="bash plain">
                happy
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="bash plain">
                happy
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="bash plain">
                Connection closed by foreign host.
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        可看到客户端接收到来自服务器端的回应内容。
       </p>
       <p>
        上面的例子还是一样，服务器端回应后就立即退出了。而一些真正的服务器像 www.oschina.net 是一直在运行的，时刻接受连接请求。
       </p>
       <p>
        也就是说服务器端应该一直处于运行状态，否则就不能成为“服务”，因此我们要让服务器端一直运行，最简单的方法就是把 accept 方法放在一个循环内。
       </p>
       <h3>
        一直在运行的服务器
       </h3>
       <p>
        对上述代码稍作改动：
       </p>
       <div>
        <div class="syntaxhighlighter python" id="highlighter_364753">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
             <div class="line number14 index13 alt1">
              14
             </div>
             <div class="line number15 index14 alt2">
              15
             </div>
             <div class="line number16 index15 alt1">
              16
             </div>
             <div class="line number17 index16 alt2">
              17
             </div>
             <div class="line number18 index17 alt1">
              18
             </div>
             <div class="line number19 index18 alt2">
              19
             </div>
             <div class="line number20 index19 alt1">
              20
             </div>
             <div class="line number21 index20 alt2">
              21
             </div>
             <div class="line number22 index21 alt1">
              22
             </div>
             <div class="line number23 index22 alt2">
              23
             </div>
             <div class="line number24 index23 alt1">
              24
             </div>
             <div class="line number25 index24 alt2">
              25
             </div>
             <div class="line number26 index25 alt1">
              26
             </div>
             <div class="line number27 index26 alt2">
              27
             </div>
             <div class="line number28 index27 alt1">
              28
             </div>
             <div class="line number29 index28 alt2">
              29
             </div>
             <div class="line number30 index29 alt1">
              30
             </div>
             <div class="line number31 index30 alt2">
              31
             </div>
             <div class="line number32 index31 alt1">
              32
             </div>
             <div class="line number33 index32 alt2">
              33
             </div>
             <div class="line number34 index33 alt1">
              34
             </div>
             <div class="line number35 index34 alt2">
              35
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                socket
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                sys
               </code>
              </div>
              <div class="line number3 index2 alt2">
              </div>
              <div class="line number4 index3 alt1">
               <code class="python plain">
                HOST
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                ''
               </code>
               <code class="python comments">
                # Symbolic name meaning all available interfaces
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python plain">
                PORT
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                8888
               </code>
               <code class="python comments">
                # Arbitrary non-privileged port
               </code>
              </div>
              <div class="line number6 index5 alt1">
              </div>
              <div class="line number7 index6 alt2">
               <code class="python plain">
                s
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket created'
               </code>
              </div>
              <div class="line number9 index8 alt2">
              </div>
              <div class="line number10 index9 alt1">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                s.bind((HOST, PORT))
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.error , msg:
               </code>
              </div>
              <div class="line number13 index12 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Bind failed. Error Code : '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python functions">
                str
               </code>
               <code class="python plain">
                (msg[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ])
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' Message '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                msg[
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number14 index13 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit()
               </code>
              </div>
              <div class="line number15 index14 alt2">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number16 index15 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket bind complete'
               </code>
              </div>
              <div class="line number17 index16 alt2">
              </div>
              <div class="line number18 index17 alt1">
               <code class="python plain">
                s.listen(
               </code>
               <code class="python value">
                10
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number19 index18 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket now listening'
               </code>
              </div>
              <div class="line number20 index19 alt1">
              </div>
              <div class="line number21 index20 alt2">
               <code class="python comments">
                #now keep talking with the client
               </code>
              </div>
              <div class="line number22 index21 alt1">
               <code class="python keyword">
                while
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number23 index22 alt2">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #wait to accept a connection - blocking call
               </code>
              </div>
              <div class="line number24 index23 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                conn, addr
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                s.accept()
               </code>
              </div>
              <div class="line number25 index24 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Connected with '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                addr[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ]
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ':'
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python functions">
                str
               </code>
               <code class="python plain">
                (addr[
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ])
               </code>
              </div>
              <div class="line number26 index25 alt1">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number27 index26 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                data
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                conn.recv(
               </code>
               <code class="python value">
                1024
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number28 index27 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                reply
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                'OK...'
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                data
               </code>
              </div>
              <div class="line number29 index28 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                if
               </code>
               <code class="python keyword">
                not
               </code>
               <code class="python plain">
                data:
               </code>
              </div>
              <div class="line number30 index29 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                break
               </code>
              </div>
              <div class="line number31 index30 alt2">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number32 index31 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                conn.sendall(reply)
               </code>
              </div>
              <div class="line number33 index32 alt2">
              </div>
              <div class="line number34 index33 alt1">
               <code class="python plain">
                conn.close()
               </code>
              </div>
              <div class="line number35 index34 alt2">
               <code class="python plain">
                s.close()
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        很简单只是加多一个 while 1 语句而已。
       </p>
       <p>
        继续运行服务器，然后打开另外三个命令行窗口。每个窗口都使用 telnet 命令连接到服务器：
       </p>
       <div>
        <div class="syntaxhighlighter shell" id="highlighter_770209">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="shell plain">
                $ telnet localhost 5000
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="shell plain">
                Trying 127.0.0.1...
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="shell plain">
                Connected to localhost.
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="shell plain">
                Escape character is
               </code>
               <code class="shell string">
                '^]'
               </code>
               <code class="shell plain">
                .
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="shell plain">
                happy
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="shell plain">
                OK .. happy
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="shell plain">
                Connection closed by foreign host.
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        服务器所在的终端窗口显示的是：
       </p>
       <div>
        <div class="syntaxhighlighter shell" id="highlighter_760678">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="shell plain">
                $ python server.py
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="shell plain">
                Socket created
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="shell plain">
                Socket bind complete
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="shell plain">
                Socket now listening
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="shell plain">
                Connected with 127.0.0.1:60225
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="shell plain">
                Connected with 127.0.0.1:60237
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="shell plain">
                Connected with 127.0.0.1:60239
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        你看服务器再也不退出了，好吧，用 Ctrl+C 关闭服务器，所有的 telnet 终端将会显示 “Connection closed by foreign host.”
       </p>
       <p>
        已经很不错了，但是这样的通讯效率太低了，服务器程序使用循环来接受连接并发送回应，这相当于是一次最多处理一个客户端的请求，而我们要求服务器可同时处理多个请求。
       </p>
       <h3>
        处理多个连接
       </h3>
       <p>
        为了处理多个连接，我们需要一个独立的处理代码在主服务器接收到连接时运行。一种方法是使用线程，服务器接收到连接然后创建一个线程来处理连接收发数据，然后主服务器程序返回去接收新的连接。
       </p>
       <p>
        下面是我们使用线程来处理连接请求：
       </p>
       <div>
        <div class="syntaxhighlighter python" id="highlighter_112449">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <caption>
           Code
          </caption>
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
             <div class="line number12 index11 alt1">
              12
             </div>
             <div class="line number13 index12 alt2">
              13
             </div>
             <div class="line number14 index13 alt1">
              14
             </div>
             <div class="line number15 index14 alt2">
              15
             </div>
             <div class="line number16 index15 alt1">
              16
             </div>
             <div class="line number17 index16 alt2">
              17
             </div>
             <div class="line number18 index17 alt1">
              18
             </div>
             <div class="line number19 index18 alt2">
              19
             </div>
             <div class="line number20 index19 alt1">
              20
             </div>
             <div class="line number21 index20 alt2">
              21
             </div>
             <div class="line number22 index21 alt1">
              22
             </div>
             <div class="line number23 index22 alt2">
              23
             </div>
             <div class="line number24 index23 alt1">
              24
             </div>
             <div class="line number25 index24 alt2">
              25
             </div>
             <div class="line number26 index25 alt1">
              26
             </div>
             <div class="line number27 index26 alt2">
              27
             </div>
             <div class="line number28 index27 alt1">
              28
             </div>
             <div class="line number29 index28 alt2">
              29
             </div>
             <div class="line number30 index29 alt1">
              30
             </div>
             <div class="line number31 index30 alt2">
              31
             </div>
             <div class="line number32 index31 alt1">
              32
             </div>
             <div class="line number33 index32 alt2">
              33
             </div>
             <div class="line number34 index33 alt1">
              34
             </div>
             <div class="line number35 index34 alt2">
              35
             </div>
             <div class="line number36 index35 alt1">
              36
             </div>
             <div class="line number37 index36 alt2">
              37
             </div>
             <div class="line number38 index37 alt1">
              38
             </div>
             <div class="line number39 index38 alt2">
              39
             </div>
             <div class="line number40 index39 alt1">
              40
             </div>
             <div class="line number41 index40 alt2">
              41
             </div>
             <div class="line number42 index41 alt1">
              42
             </div>
             <div class="line number43 index42 alt2">
              43
             </div>
             <div class="line number44 index43 alt1">
              44
             </div>
             <div class="line number45 index44 alt2">
              45
             </div>
             <div class="line number46 index45 alt1">
              46
             </div>
             <div class="line number47 index46 alt2">
              47
             </div>
             <div class="line number48 index47 alt1">
              48
             </div>
             <div class="line number49 index48 alt2">
              49
             </div>
             <div class="line number50 index49 alt1">
              50
             </div>
             <div class="line number51 index50 alt2">
              51
             </div>
             <div class="line number52 index51 alt1">
              52
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                socket
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python keyword">
                import
               </code>
               <code class="python plain">
                sys
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python keyword">
                from
               </code>
               <code class="python plain">
                thread
               </code>
               <code class="python keyword">
                import
               </code>
               <code class="python keyword">
                *
               </code>
              </div>
              <div class="line number4 index3 alt1">
              </div>
              <div class="line number5 index4 alt2">
               <code class="python plain">
                HOST
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                ''
               </code>
               <code class="python comments">
                # Symbolic name meaning all available interfaces
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python plain">
                PORT
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python value">
                8888
               </code>
               <code class="python comments">
                # Arbitrary non-privileged port
               </code>
              </div>
              <div class="line number7 index6 alt2">
              </div>
              <div class="line number8 index7 alt1">
               <code class="python plain">
                s
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                socket.socket(socket.AF_INET, socket.SOCK_STREAM)
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket created'
               </code>
              </div>
              <div class="line number10 index9 alt1">
              </div>
              <div class="line number11 index10 alt2">
               <code class="python comments">
                #Bind socket to local host and port
               </code>
              </div>
              <div class="line number12 index11 alt1">
               <code class="python keyword">
                try
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number13 index12 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                s.bind((HOST, PORT))
               </code>
              </div>
              <div class="line number14 index13 alt1">
               <code class="python keyword">
                except
               </code>
               <code class="python plain">
                socket.error , msg:
               </code>
              </div>
              <div class="line number15 index14 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Bind failed. Error Code : '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python functions">
                str
               </code>
               <code class="python plain">
                (msg[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ])
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ' Message '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                msg[
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ]
               </code>
              </div>
              <div class="line number16 index15 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                sys.exit()
               </code>
              </div>
              <div class="line number17 index16 alt2">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number18 index17 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket bind complete'
               </code>
              </div>
              <div class="line number19 index18 alt2">
              </div>
              <div class="line number20 index19 alt1">
               <code class="python comments">
                #Start listening on socket
               </code>
              </div>
              <div class="line number21 index20 alt2">
               <code class="python plain">
                s.listen(
               </code>
               <code class="python value">
                10
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number22 index21 alt1">
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Socket now listening'
               </code>
              </div>
              <div class="line number23 index22 alt2">
              </div>
              <div class="line number24 index23 alt1">
               <code class="python comments">
                #Function for handling connections. This will be used to create threads
               </code>
              </div>
              <div class="line number25 index24 alt2">
               <code class="python keyword">
                def
               </code>
               <code class="python plain">
                clientthread(conn):
               </code>
              </div>
              <div class="line number26 index25 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #Sending message to connected client
               </code>
              </div>
              <div class="line number27 index26 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                conn.send(
               </code>
               <code class="python string">
                'Welcome to the server. Type something and hit enter\n'
               </code>
               <code class="python plain">
                )
               </code>
               <code class="python comments">
                #send only takes string
               </code>
              </div>
              <div class="line number28 index27 alt1">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number29 index28 alt2">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #infinite loop so that function do not terminate and thread do not end.
               </code>
              </div>
              <div class="line number30 index29 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                while
               </code>
               <code class="python color1">
                True
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number31 index30 alt2">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number32 index31 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #Receiving from client
               </code>
              </div>
              <div class="line number33 index32 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                data
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                conn.recv(
               </code>
               <code class="python value">
                1024
               </code>
               <code class="python plain">
                )
               </code>
              </div>
              <div class="line number34 index33 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                reply
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python string">
                'OK...'
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                data
               </code>
              </div>
              <div class="line number35 index34 alt2">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                if
               </code>
               <code class="python keyword">
                not
               </code>
               <code class="python plain">
                data:
               </code>
              </div>
              <div class="line number36 index35 alt1">
               <code class="python spaces">
               </code>
               <code class="python keyword">
                break
               </code>
              </div>
              <div class="line number37 index36 alt2">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number38 index37 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                conn.sendall(reply)
               </code>
              </div>
              <div class="line number39 index38 alt2">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number40 index39 alt1">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #came out of loop
               </code>
              </div>
              <div class="line number41 index40 alt2">
               <code class="python spaces">
               </code>
               <code class="python plain">
                conn.close()
               </code>
              </div>
              <div class="line number42 index41 alt1">
              </div>
              <div class="line number43 index42 alt2">
               <code class="python comments">
                #now keep talking with the client
               </code>
              </div>
              <div class="line number44 index43 alt1">
               <code class="python keyword">
                while
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                :
               </code>
              </div>
              <div class="line number45 index44 alt2">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #wait to accept a connection - blocking call
               </code>
              </div>
              <div class="line number46 index45 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                conn, addr
               </code>
               <code class="python keyword">
                =
               </code>
               <code class="python plain">
                s.accept()
               </code>
              </div>
              <div class="line number47 index46 alt2">
               <code class="python spaces">
               </code>
               <code class="python functions">
                print
               </code>
               <code class="python string">
                'Connected with '
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python plain">
                addr[
               </code>
               <code class="python value">
                0
               </code>
               <code class="python plain">
                ]
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python string">
                ':'
               </code>
               <code class="python keyword">
                +
               </code>
               <code class="python functions">
                str
               </code>
               <code class="python plain">
                (addr[
               </code>
               <code class="python value">
                1
               </code>
               <code class="python plain">
                ])
               </code>
              </div>
              <div class="line number48 index47 alt1">
               <code class="python spaces">
               </code>
              </div>
              <div class="line number49 index48 alt2">
               <code class="python spaces">
               </code>
               <code class="python comments">
                #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
               </code>
              </div>
              <div class="line number50 index49 alt1">
               <code class="python spaces">
               </code>
               <code class="python plain">
                start_new_thread(clientthread ,(conn,))
               </code>
              </div>
              <div class="line number51 index50 alt2">
              </div>
              <div class="line number52 index51 alt1">
               <code class="python plain">
                s.close()
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        运行上述服务端程序，然后像之前一样打开三个终端窗口并执行 telent 命令：
       </p>
       <div>
        <div class="syntaxhighlighter shell" id="highlighter_949408">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
             <div class="line number7 index6 alt2">
              7
             </div>
             <div class="line number8 index7 alt1">
              8
             </div>
             <div class="line number9 index8 alt2">
              9
             </div>
             <div class="line number10 index9 alt1">
              10
             </div>
             <div class="line number11 index10 alt2">
              11
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="shell plain">
                $ telnet localhost 8888
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="shell plain">
                Trying 127.0.0.1...
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="shell plain">
                Connected to localhost.
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="shell plain">
                Escape character is
               </code>
               <code class="shell string">
                '^]'
               </code>
               <code class="shell plain">
                .
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="shell plain">
                Welcome to the server. Type something and hit enter
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="shell plain">
                hi
               </code>
              </div>
              <div class="line number7 index6 alt2">
               <code class="shell plain">
                OK...hi
               </code>
              </div>
              <div class="line number8 index7 alt1">
               <code class="shell plain">
                asd
               </code>
              </div>
              <div class="line number9 index8 alt2">
               <code class="shell plain">
                OK...asd
               </code>
              </div>
              <div class="line number10 index9 alt1">
               <code class="shell plain">
                cv
               </code>
              </div>
              <div class="line number11 index10 alt2">
               <code class="shell plain">
                OK...cv
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        服务器端所在终端窗口输出信息如下：
       </p>
       <div>
        <div class="syntaxhighlighter python" id="highlighter_793103">
         <div class="toolbar">
          <a class="toolbar_item command_help help" href="http://www.oschina.net/question/12_76126">
           ?
          </a>
         </div>
         <table border="0" cellpadding="0" cellspacing="0">
          <tbody>
           <tr>
            <td class="gutter">
             <div class="line number1 index0 alt2">
              1
             </div>
             <div class="line number2 index1 alt1">
              2
             </div>
             <div class="line number3 index2 alt2">
              3
             </div>
             <div class="line number4 index3 alt1">
              4
             </div>
             <div class="line number5 index4 alt2">
              5
             </div>
             <div class="line number6 index5 alt1">
              6
             </div>
            </td>
            <td class="code">
             <div class="container">
              <div class="line number1 index0 alt2">
               <code class="python plain">
                $ python server.py
               </code>
              </div>
              <div class="line number2 index1 alt1">
               <code class="python plain">
                Socket created
               </code>
              </div>
              <div class="line number3 index2 alt2">
               <code class="python plain">
                Socket bind complete
               </code>
              </div>
              <div class="line number4 index3 alt1">
               <code class="python plain">
                Socket now listening
               </code>
              </div>
              <div class="line number5 index4 alt2">
               <code class="python plain">
                Connected with
               </code>
               <code class="python value">
                127.0
               </code>
               <code class="python plain">
                .
               </code>
               <code class="python value">
                0.1
               </code>
               <code class="python plain">
                :
               </code>
               <code class="python value">
                60730
               </code>
              </div>
              <div class="line number6 index5 alt1">
               <code class="python plain">
                Connected with
               </code>
               <code class="python value">
                127.0
               </code>
               <code class="python plain">
                .
               </code>
               <code class="python value">
                0.1
               </code>
               <code class="python plain">
                :
               </code>
               <code class="python value">
                60731
               </code>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        线程接管了连接并返回相应数据给客户端。
       </p>
       <p>
        这便是我们所要介绍的服务器端编程。
       </p>
       <h3>
        结论
       </h3>
       <p>
        到这里为止，你已经学习了 Python 的 Socket 基本编程，你可自己动手编写一些例子来强化这些知识。
       </p>
       <p>
        你可能会遇见一些问题：Bind failed. Error Code : 98 Message Address already in use，碰见这种问题只需要简单更改服务器端口即可。
       </p>
       <p>
        <a href="http://www.binarytides.com/python-socket-programming-tutorial/" rel="nofollow" target="_blank">
         英文原文
        </a>
        ，
        <a href="http://www.oschina.net/question/12_76126" rel="nofollow" target="_blank">
         OSCHINA
        </a>
        原创翻译
       </p>
      </div>
      <div>
       <strong>
        注：转载文章均来自于公开网络，仅供学习使用，不会用于任何商业用途，如果侵犯到原作者的权益，请您与我们联系删除或者授权事宜，联系邮箱：contact@dataunion.org。转载数盟网站文章请注明原文章作者，否则产生的任何版权纠纷与数盟无关。
       </strong>
      </div>
      <!--content_text-->
      <div class="fenxian">
       <!-- JiaThis Button BEGIN -->
       <div class="jiathis_style_32x32">
        <p class="jiathis_button_weixin">
        </p>
        <p class="jiathis_button_tsina">
        </p>
        <p class="jiathis_button_qzone">
        </p>
        <p class="jiathis_button_cqq">
        </p>
        <p class="jiathis_button_tumblr">
        </p>
        <a class="jiathis jiathis_txt jtico jtico_jiathis" href="http://www.jiathis.com/share" target="_blank">
        </a>
        <p class="jiathis_counter_style">
        </p>
       </div>
       <!-- JiaThis Button END -->
      </div>
     </article>
     <!--content-->
     <!--相关文章-->
     <div class="xianguan">
      <div class="xianguantitle">
       相关文章！
      </div>
      <ul class="pic">
       <li>
        <a href="http://dataunion.org/24678.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/20140917125452915416-216x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/24678.html" rel="bookmark" title="python3中的正则模块">
         python3中的正则模块
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/24675.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/t015b337bd75d9ef893-161x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/24675.html" rel="bookmark" title="注释是恶魔，请不要再写一行注释">
         注释是恶魔，请不要再写一行注释
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/24660.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/u16130037972892789947fm21gp0-300x157.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/24660.html" rel="bookmark" title="如何从Github上轻松安装R包">
         如何从Github上轻松安装R包
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/24654.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/df53fac99fc53ba5a90666abcca25e6d_b-267x200.png"/>
        </a>
        <a class="link" href="http://dataunion.org/24654.html" rel="bookmark" title="简单形象又有趣地说说强大的神经网络">
         简单形象又有趣地说说强大的神经网络
        </a>
       </li>
      </ul>
     </div>
     <!--相关文章-->
     <div class="comment" id="comments">
      <!-- You can start editing here. -->
      <!-- If comments are open, but there are no comments. -->
      <div class="title">
       期待你一针见血的评论，Come on！
      </div>
      <div id="respond">
       <p>
        不用想啦，马上
        <a href="http://dataunion.org/wp-login.php?redirect_to=http%3A%2F%2Fdataunion.org%2F24438.html">
         "登录"
        </a>
        发表自已的想法.
       </p>
      </div>
     </div>
     <!-- .nav-single -->
    </div>
    <!--Container End-->
    <aside id="sitebar">
     <div class="sitebar_list2">
      <div class="wptag">
       <span class="tagtitle">
        热门标签+
       </span>
       <div class="tagg">
        <ul class="menu" id="menu-%e5%8f%8b%e6%83%85%e9%93%be%e6%8e%a5">
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1605" id="menu-item-1605">
          <a href="http://taidizh.com/">
           泰迪智慧
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20884" id="menu-item-20884">
          <a href="http://www.transwarp.cn/">
           星环科技
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-3538" id="menu-item-3538">
          <a href="http://datall.org/">
           珈和遥感
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20888" id="menu-item-20888">
          <a href="http://www.chinahadoop.cn/">
           小象学院
          </a>
         </li>
        </ul>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <div class="textwidget">
       <div align="center">
        <a href="http://study.163.com/course/courseMain.htm?courseId=991022" target="_blank">
         <img src="http://dataunion.org/wp-content/uploads/2016/03/dv.jpg"/>
        </a>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       文章分类
      </h4>
      <div class="tagcloud">
       <a class="tag-link-44" href="http://dataunion.org/category/industry/demo" style="font-size: 10.204724409449pt;" title="4个话题">
        Demo展示
       </a>
       <a class="tag-link-31" href="http://dataunion.org/category/experts" style="font-size: 15.826771653543pt;" title="52个话题">
        专家团队
       </a>
       <a class="tag-link-870" href="http://dataunion.org/category/tech/ai" style="font-size: 19.795275590551pt;" title="273个话题">
        人工智能
       </a>
       <a class="tag-link-488" href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f" style="font-size: 8pt;" title="1个话题">
        加入数盟
       </a>
       <a class="tag-link-869" href="http://dataunion.org/category/tech/viz" style="font-size: 17.204724409449pt;" title="93个话题">
        可视化
       </a>
       <a class="tag-link-30" href="http://dataunion.org/category/partners" style="font-size: 10.645669291339pt;" title="5个话题">
        合作伙伴
       </a>
       <a class="tag-link-889" href="http://dataunion.org/category/parterc" style="font-size: 11.582677165354pt;" title="8个话题">
        合作会议
       </a>
       <a class="tag-link-104" href="http://dataunion.org/category/books" style="font-size: 12.96062992126pt;" title="15个话题">
        图书
       </a>
       <a class="tag-link-220" href="http://dataunion.org/category/tech/base" style="font-size: 19.850393700787pt;" title="281个话题">
        基础架构
       </a>
       <a class="tag-link-219" href="http://dataunion.org/category/tech/analysis" style="font-size: 19.409448818898pt;" title="232个话题">
        数据分析
       </a>
       <a class="tag-link-887" href="http://dataunion.org/category/tech/dm" style="font-size: 13.291338582677pt;" title="17个话题">
        数据挖掘
       </a>
       <a class="tag-link-34" href="http://dataunion.org/category/tech" style="font-size: 20.732283464567pt;" title="404个话题">
        文章
       </a>
       <a class="tag-link-1" href="http://dataunion.org/category/uncategorized" style="font-size: 22pt;" title="693个话题">
        未分类
       </a>
       <a class="tag-link-4" href="http://dataunion.org/category/events" style="font-size: 14.503937007874pt;" title="29个话题">
        活动
       </a>
       <a class="tag-link-890" href="http://dataunion.org/category/tech/%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0" style="font-size: 10.204724409449pt;" title="4个话题">
        深度学习
       </a>
       <a class="tag-link-221" href="http://dataunion.org/category/tech/devl" style="font-size: 18.968503937008pt;" title="193个话题">
        编程语言
       </a>
       <a class="tag-link-888" href="http://dataunion.org/category/career" style="font-size: 15.661417322835pt;" title="48个话题">
        职业规划
       </a>
       <a class="tag-link-5" href="http://dataunion.org/category/jobs" style="font-size: 14.11811023622pt;" title="25个话题">
        职位
       </a>
       <a class="tag-link-871" href="http://dataunion.org/category/industry" style="font-size: 15.716535433071pt;" title="49个话题">
        行业
       </a>
       <a class="tag-link-613" href="http://dataunion.org/category/industry/case" style="font-size: 16.984251968504pt;" title="84个话题">
        行业应用
       </a>
       <a class="tag-link-885" href="http://dataunion.org/category/industry/news" style="font-size: 17.425196850394pt;" title="102个话题">
        行业资讯
       </a>
       <a class="tag-link-10" href="http://dataunion.org/category/training" style="font-size: 14.228346456693pt;" title="26个话题">
        课程
       </a>
       <a class="tag-link-16" href="http://dataunion.org/category/sources" style="font-size: 15.661417322835pt;" title="48个话题">
        资源
       </a>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       功能
      </h4>
      <ul>
       <li>
        <a href="http://dataunion.org/wp-login.php?action=register">
         注册
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/wp-login.php">
         登录
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/feed">
         文章
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/comments/feed">
         评论
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="https://cn.wordpress.org/" title="基于WordPress，一个优美、先进的个人信息发布平台。">
         WordPress.org
        </a>
       </li>
      </ul>
     </div>
    </aside>
    <div class="clear">
    </div>
   </div>
   <!--main-->
   ﻿
   <footer id="dibu">
    <div class="about">
     <div class="right">
      <ul class="menu" id="menu-%e5%ba%95%e9%83%a8%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-18024" id="menu-item-18024">
        <a href="http://dataunion.org/category/partners">
         合作伙伴
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20881" id="menu-item-20881">
        <a href="http://dataunion.org/contribute">
         文章投稿
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20872" id="menu-item-20872">
        <a href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f">
         加入数盟
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22441" id="menu-item-22441">
        <a href="http://dataunion.org/f-links">
         友情链接
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20874" id="menu-item-20874">
        <a href="http://dataunion.org/aboutus">
         关于数盟
        </a>
       </li>
      </ul>
      <p class="banquan">
       数盟社区        ，
        做最棒的数据科学社区
      </p>
     </div>
     <div class="left">
      <ul class="bottomlist">
       <li>
        <a href="http://weibo.com/DataScientistUnion  " target="_blank" 　title="">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weibo.png"/>
        </a>
       </li>
       <li>
        <a class="cd-popup-trigger" href="http://dataunion.org/24438.html#0">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weixin.png"/>
        </a>
       </li>
      </ul>
      <div class="cd-popup">
       <div class="cd-popup-container">
        <h1>
         扫描二维码,加微信公众号
        </h1>
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/2014-12-06-1515289049.png"/>
        <a class="cd-popup-close" href="http://dataunion.org/24438.html">
        </a>
       </div>
       <!-- cd-popup-container -->
      </div>
      <!-- cd-popup -->
     </div>
    </div>
    <!--about-->
    <div class="bottom">
     <a href="http://dataunion.org/">
      数盟社区
     </a>
     <a href="http://www.miitbeian.gov.cn/" rel="external nofollow" target="_blank">
      京ICP备14026740号
     </a>
     联系我们：
     <a href="mailto:contact@dataunion.org" target="_blank">
      contact@dataunion.org
     </a>
     <div class="tongji">
     </div>
     <!--bottom-->
     <div class="scroll" id="scroll" style="display:none;">
      ︿
     </div>
    </div>
   </footer>
   <!--dibu-->
  </div>
 </body>
</html>